app.controller("bookListCtrl", function($scope, $http, $state, $stateParams) {
	
	
	$http({
		method: "GET",
		url: "/hake/bookStore/data/books" + $stateParams.bookType + ".json"
	}).success(function(data) {
		getPage(1, $scope.gridOptions.pagingOptions.pageSize, data)
		
		//$scope.gridOptions.data = data ;
		
	}).error(function() {
		console.info("error") ;
	});
	
	var getPage = function(curPage, pageSize, data) {
        var firstRow = (curPage - 1) * pageSize;
        $scope.gridOptions.totalItems = data.length;
        console.info(data.slice(firstRow, firstRow + pageSize)) ;
        $scope.gridOptions.data = data.slice(firstRow, firstRow + pageSize);
        
	};

	
	$scope.gridOptions = {
			pagingOptions: {
				pageSizes: [5, 10, 20],
		        pageSize: 2,
		        currentPage: 1
			},
			columnDefs: [{
	            field: 'index',
	            displayName: '序号',
	            width: 60,
	            pinnable: false,
	            sortable: false
	        }, {
	            field: 'name',
	            displayName: '书名',
	            enableCellEdit: true
	        }, {
	            field: 'author',
	            displayName: '作者',
	            enableCellEdit: true,
	            width: 220
	        }, {
	            field: 'pubTime',
	            displayName: '出版日期',
	            enableCellEdit: true,
	            width: 120
	        }, {
	            field: 'price',
	            displayName: '定价',
	            enableCellEdit: true,
	            width: 120,
	            cellFilter: 'currency:"$"'
	        }, {
	            field: 'bookId',
	            displayName: '操作',
	            enableCellEdit: false,
	            sortable: false,
	            pinnable: false,
	            cellTemplate: '<div><a ui-sref="bookdetail({bookId:row.getProperty(col.field)})" id="{{row.getProperty(col.field)}}">详情</a></div>'
	        }]
	};
	/*$scope.gridOptions = {
	        enableSorting: true,
	        columnDefs: [
	          { name:'firstName', field: 'first-name' },
	          { name:'1stFriend', field: 'friends[0]' },
	          { name:'city', field: 'address.city'},
	          { name:'getZip', field: 'getZip()', enableCellEdit:false}
	        ],
	        data : [      {
	                           "first-name": "Cox",
	                           "friends": ["friend0"],
	                           "address": {street:"301 Dove Ave", city:"Laurel", zip:"39565"},
	                           "getZip" : function() {return this.address.zip;}
	                       }
	                   ]
	      };*/
	 
});